package com.example.ccj.b.c.recycler;

import android.content.res.Resources;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.ccj.R;
import com.example.ccj.u.StringUtils;

import java.util.List;

/**
 * 默认RecyclerView的Adapter
 * 主要实现了右滑菜单
 * 驰郡
 * wechat:chijun9
 * 2023/11/18
*/
public class DefaultAdapter<T> extends RecyclerView.Adapter<DefaultAdapter.ViewHolder>{
    int layout;
    //数据
    private List<T> defaultData;
    //获取放入控件的view
    DefaultAdapterOnBind<T> defaultAdapterOnBind;
    //功能按钮设置
    List<FunctionModel<T>> functionModels;
    //功能按钮宽度
    private static final int functionWidth = 150;
    //默认背景颜色列表
    private static final int[] backColor = {
            Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120),
            Color.rgb(106, 167, 134), Color.rgb(53, 194, 209)
    };

    public DefaultAdapter() {}

    /**
     *
     * @param defaultData 数据
     * @param layout 布局
     * @param defaultAdapterOnBind 布局加载回调
     */
    public DefaultAdapter(List<T> defaultData, int layout, DefaultAdapterOnBind<T> defaultAdapterOnBind) {
        this.defaultData = defaultData;
        this.defaultAdapterOnBind = defaultAdapterOnBind;
        this.layout = layout;
    }

    /**
     *
     * @param defaultData 数据
     * @param functionModels 功能按钮定义
     * @param layout 布局
     * @param defaultAdapterOnBind 布局加载回调
     */
    public DefaultAdapter(List<T> defaultData, List<FunctionModel<T>> functionModels, int layout, DefaultAdapterOnBind<T> defaultAdapterOnBind) {
        this.defaultData = defaultData;
        this.defaultAdapterOnBind = defaultAdapterOnBind;
        this.functionModels = functionModels;
        this.layout = layout;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_default, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        T t = defaultData.get(position);
        holder.pot.removeAllViews();
        View view = defaultAdapterOnBind.onBind(LayoutInflater.from(holder.pot.getContext()).inflate(layout, null), t);
        if (view == null) view = defaultAdapterOnBind.onBind(LayoutInflater.from(holder.pot.getContext()).inflate(layout, null), t,position);
        holder.pot.addView(view);

        if (functionModels != null) {
            holder.function.removeAllViews();
            int wg = 0;//功能空间总宽度计算
            int i = 0;//用于背景色选择
            for (FunctionModel<T> functionModel : functionModels) {
                if (i > backColor.length) i = 0;
                if (functionModel.layout == 0) {
                    TextView textView = new TextView(holder.function.getContext());
                    //设置名称
                    if (StringUtils.isEmpty(functionModel.name)) textView.setText("");
                    else textView.setText(functionModel.name);
                    //背景颜色，如果颜色未设置，设置固定颜色
                    if (functionModel.color == 0) textView.setBackgroundColor(backColor[i]);
                    else textView.setBackgroundColor(functionModel.color);
                    //设置点击事件回调
                    final int pp = position;
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (functionModel.function != null) functionModel.function.onClick(t,pp);
                        }
                    });
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(functionWidth, ViewGroup.LayoutParams.MATCH_PARENT);
                    layoutParams.gravity = Gravity.CENTER;//布局居中
                    textView.setLayoutParams(layoutParams);
                    textView.setGravity(Gravity.CENTER);//文字居中
                    wg += functionWidth;
                    holder.function.addView(textView);
                } else {
                    View inflate = LayoutInflater.from(holder.function.getContext()).inflate(functionModel.layout, null);
                    wg += inflate.getLayoutParams().width;
                    holder.function.addView(inflate);
                }
                i++;
            }
            ViewGroup.LayoutParams layoutParams = holder.function.getLayoutParams();
            layoutParams.width = wg;
        }
    }

    @Override
    public int getItemCount() {
        return defaultData == null ? 0 : defaultData.size();
    }

    static class ViewHolder extends  RecyclerView.ViewHolder{
        private final LinearLayout pot,function;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            pot = itemView.findViewById(R.id.default_adapter_pot);
            function = itemView.findViewById(R.id.default_adapter_function);


            if (itemView.getBackground() == null) {
                TypedValue typedValue = new TypedValue();
                Resources.Theme theme = itemView.getContext().getTheme();
                int top = itemView.getPaddingTop();
                int bottom = itemView.getPaddingBottom();
                int left = itemView.getPaddingLeft();
                int right = itemView.getPaddingRight();
                if (theme.resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true)) {
                    itemView.setBackgroundResource(typedValue.resourceId);
                }
                itemView.setPadding(left, top, right, bottom);
            }
        }
    }
}
